# -*- coding:utf-8 -*-

import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import timedelta
import datetime
import requests
from common.my_time import MyTime
from common.attr_v3 import AttrV3
from database.db_handler import MysqlHander
import json
import os
from selenium import webdriver
from webdriver_base import WebdriverBase
from common.my_file import MyFile

'''
北交所监管数据
'''

'''
chromedriver = "D:\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(executable_path="D:\\chromedriver.exe")

file = open('./out/beijiaosuo_wenxun_data.txt', 'w', encoding = 'utf-8')
'''

class BjWenxunData(WebdriverBase):
    def __init__(self):
        self.v3 = AttrV3()
        self.file = open('./txt/' + os.path.split(__file__)[-1].split(".")[0] + ".txt", 'w', encoding = 'utf-8')
        WebdriverBase.__init__(self, './log/' + os.path.split(__file__)[-1].split(".")[0] + ".log")

    def __del__(self):
        self.file.close()
        pass

    def query_page_data(self, page):
        time.sleep(10)
        trs = self.driver.find_element_by_tag_name("tbody").find_elements_by_tag_name("tr")
        for tr in trs:
            tds = tr.find_elements_by_tag_name("td")
            data_code = tds[0].text
            data_url = tds[2].find_element_by_tag_name("a").get_attribute("href")
            data_title = tds[2].find_element_by_tag_name("a").text
            data_date = tds[4].text

            table = self.v3.get_table_name(3002)
            try:
                self.v3.add_attr_data(table, data_code, data_title, data_date, data_url, "北交所")
            except Exception as e:
                self.write_log(str(e))
                pass
            self.file.write(data_code + "|0|3002|" + data_title + "|" + data_date + "|" + data_url + "|北交所|\n")
            reply = tds[5].find_element_by_tag_name("a")
            if reply:
                data_reply_title = tds[2].find_element_by_tag_name("a").text + "[回复]"
                data_reply_url = reply.get_attribute("href")

                try:
                    self.v3.add_attr_data(table, data_code, data_reply_title, data_date, data_reply_url, "北交所")
                except Exception as e:
                    self.write_log(str(e))
                    pass
                self.file.write(data_code + "|0|3002|" + data_reply_title + "|" + data_date + "|" + data_reply_url + "|北交所|\n")
        
    def query_data(self):
        self.driver.get("http://www.bse.cn/disclosure/enquiry_letter.html")
        page = 0
        self.query_page_data(page)
        self.driver.quit()
    
    def get_data(self):
        self.query_data()

def beijiaosuo_wenxun_api():
    d = BjWenxunData()
    d.get_data()


if __name__ == '__main__':
    d = BjWenxunData()
    d.get_data()

